package com.bjsj.pro.base;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.util.Linkify;
import android.util.SparseArray;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.widget.Checkable;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bjsj.pro.R;


/**
 * 类说明：复用ViewHolder, 绑定 RecyclerView item 的控件
 * Author: gaobaiq
 * Date: 2016/3/29 16:02
 */
public class BaseViewHolder {

    private SparseArray<View> mViews;
    private View view;

    public static BaseViewHolder getViewHolder(View view) {
        BaseViewHolder baseViewHolder = (BaseViewHolder) view.getTag();
        if (baseViewHolder == null) {
            baseViewHolder = new BaseViewHolder(view);
            view.setTag(baseViewHolder);
        }
        return baseViewHolder;
    }

    private BaseViewHolder(View view) {
        this.view = view;
        mViews = new SparseArray<>();
        view.setTag(mViews);
    }

    public <T extends View> T get(int viewId) {
        View view = mViews.get(viewId);
        if (view == null) {
            view = this.view.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }

    public View getConvertView() {
        return view;
    }

    /**
     * 关于值的设置
     */
    //设置文字资源
    public BaseViewHolder setText(int viewId, String text) {
        TextView tv = get(viewId);
        tv.setText(text);
        return this;
    }

    //设置文字资源
    public BaseViewHolder setText(int viewId, int resId) {
        TextView tv = get(viewId);
        tv.setText(resId);
        return this;
    }

    //设置图片资源 --资源ID
    public BaseViewHolder setImageResource(int viewId, int resId) {
        ImageView view = get(viewId);
        view.setImageResource(resId);
        return this;
    }

    //设置图片资源 --bitmap
    public BaseViewHolder setImageBitmap(int viewId, Bitmap bitmap) {
        ImageView view = get(viewId);
        view.setImageBitmap(bitmap);
        return this;
    }

    //设置图片资源 --drawable
    public BaseViewHolder setImageDrawable(int viewId, Drawable drawable) {
        ImageView view = get(viewId);
        view.setImageDrawable(drawable);
        return this;
    }

    //设置图片资源 --drawable
    public BaseViewHolder setImageWithGlide(int viewId, Context mContext, String loadRes) {
        ImageView view = get(viewId);
        Glide.with(mContext).load(loadRes)
                .asBitmap()
                .centerCrop().placeholder(R.drawable.pic_zhanwei_mid)
                .error(R.drawable.pic_zhanwei_mid)
                .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存全尺寸
                .into(view);
//        Glide.with(mContext).load(loadRes)
////                .placeholder(R.drawable.pic_zhanwei_mid)
//                .error(R.drawable.pic_zhanwei_mid)
//                .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存全尺寸
//                .crossFade()//淡入淡出效果
//                .into(view);


        return this;
    }

    //设置图片资源 --drawable
    public BaseViewHolder setImageWithGlide(int viewId, Context mContext, int loadRes) {
        ImageView view = get(viewId);

        Glide.with(mContext)
                .load(loadRes)
                .error(R.drawable.pic_zhanwei_mid)
                .into(view);

        return this;
    }

    //设置颜色资源
    public BaseViewHolder setBackgroundColor(int viewId, int color) {
        View view = get(viewId);
        view.setBackgroundColor(color);
        return this;
    }

    //设置背景
    public BaseViewHolder setBackgroundRes(int viewId, int backgroundRes) {
        View view = get(viewId);
        view.setBackgroundResource(backgroundRes);
        return this;
    }

    //文字颜色
    public BaseViewHolder setTextColor(int viewId, int textColor) {
        TextView view = get(viewId);
        view.setTextColor(textColor);
        return this;
    }

    //设置透明度
    @SuppressLint("NewApi")
    public BaseViewHolder setAlpha(int viewId, float value) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            get(viewId).setAlpha(value);
        } else {
            // Pre-honeycomb hack to set Alpha value
            AlphaAnimation alpha = new AlphaAnimation(value, value);
            alpha.setDuration(0);
            alpha.setFillAfter(true);
            get(viewId).startAnimation(alpha);
        }
        return this;
    }

    //设置是否可见---布尔值 ture--可见  false--控件gone
    public BaseViewHolder setVisible(int viewId, boolean visible) {
        View view = get(viewId);
        view.setVisibility(visible ? View.VISIBLE : View.GONE);
        return this;
    }

    //设置是否可见---传入可见状态
    public BaseViewHolder setVisible(int viewId, int visible) {
        View view = get(viewId);
        view.setVisibility(visible);
        return this;
    }

    //textview 设置超链接
    public BaseViewHolder linkify(int viewId) {
        TextView view = get(viewId);
        Linkify.addLinks(view, Linkify.ALL);
        return this;
    }

    //设置字体
    public BaseViewHolder setTypeface(Typeface typeface, int... viewIds) {
        for (int viewId : viewIds) {
            TextView view = get(viewId);
            view.setTypeface(typeface);
            view.setPaintFlags(view.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG);
        }
        return this;
    }

    public BaseViewHolder setProgress(int viewId, int progress) {
        ProgressBar view = get(viewId);
        view.setProgress(progress);
        return this;
    }

    public BaseViewHolder setProgress(int viewId, int progress, int max) {
        ProgressBar view = get(viewId);
        view.setMax(max);
        view.setProgress(progress);
        return this;
    }

    public BaseViewHolder setMax(int viewId, int max) {
        ProgressBar view = get(viewId);
        view.setMax(max);
        return this;
    }

    public BaseViewHolder setRating(int viewId, float rating) {
        RatingBar view = get(viewId);
        view.setRating(rating);
        return this;
    }

    public BaseViewHolder setRating(int viewId, float rating, int max) {
        RatingBar view = get(viewId);
        view.setMax(max);
        view.setRating(rating);
        return this;
    }

    public BaseViewHolder setTag(int viewId, Object tag) {
        View view = get(viewId);
        view.setTag(tag);
        return this;
    }

    public BaseViewHolder setTag(int viewId, int key, Object tag) {
        View view = get(viewId);
        view.setTag(key, tag);
        return this;
    }

    public BaseViewHolder setChecked(int viewId, boolean checked) {
        Checkable view = get(viewId);
        view.setChecked(checked);
        return this;
    }

}
